package com.example.user.controller;


import com.example.user.common.Result;
import com.example.user.entity.Product;
import com.example.user.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:8080")
public class ProductController {


    @Autowired
    private ProductService productService;

    /**
     * 首页商品展示功能
     */

    @GetMapping("/productSelectPage")
    public Result<PageInfo> productSelectPage(@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Product> productList = productService.productSelectPage();
        return Result.success(new PageInfo<>(productList));
    }

    /**
     * 商品搜索功能，按照名称搜
     */

    @GetMapping("/searchByName")
    public Result<PageInfo> searchByName(@RequestParam String name, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(1, 10);
        List<Product> productList = productService.searchByName(name);
        return Result.success(new PageInfo<>(productList));
    }

    /**
     * 按照类别进行搜索
     */


    @GetMapping("/getProductByCategory")
    public Result<PageInfo> searchByCategory(@RequestParam String category, @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "10") Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Product> productList = productService.searchByCategory(category);
        return Result.success(new PageInfo<>(productList));
    }
}
